<template>
  <div>
    <van-nav-bar
  title="账号登录"
  left-text="<"
  @click-left="onClickLeft"
/>
<van-form   @submit="onSubmit">
  <van-field
    v-model="form.username"
    name="用户名"
    placeholder="请输入账号"
    :rules="[
    { required: true, message: '请填写用户名' },
    {pattern:/^[\d\w]{6}$/, message: '用户名为6位的字母或数字'}
    ]"
  />
  <van-field
    v-model="form.password"
    type="password"
    name="密码"
    placeholder="请输入密码"
    :rules="[{ required: true, message: '请填写密码' },
     {pattern:/^[\d\w]{5,12}$/,message: '密码长度为6-12位'}]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info"  native-type="submit" >提交</van-button>
  </div>
</van-form>
<div id="login">
  <router-link to="/logon" >还没有账号去注册~</router-link>
</div>
  </div>
</template>

<script>
import axios from 'axios'
axios.defaults.baseURL = 'http://liufusong.top:8080'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },

  created () {

  },
  // 账号: hkzf66 密码: test2
  methods: {
    onClickLeft () {
      this.$router.back('/login')
    },
    async  onSubmit () {
      const res = await axios({
        method: 'POST',
        url: '/user/login',
        data: {
          username: this.form.username,
          password: this.form.password
        }
      })
      if (res.status === 200) {
        this.$router.push('/layout/home')
      }
    }
  }
}
</script>

<style scoped lang='less'>
#login{
  text-align: center;
}
</style>
